﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChurchData.Business
{
    public class LKMaritalStatusBO : ChurchDataBusinessBase, ILKTable
    {
        #region ILKTable Members
        public List<GenericLKTableResults> GetActiveRecords()
        {
            return this.GetActiveRecords(-1);
        }

        public List<GenericLKTableResults> GetActiveRecords(int? grandFatherPK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());
            var x =
                    from oLK in oDB.LKMaritalStatus
                    where oLK.ActiveInd == true || (grandFatherPK != null && grandFatherPK == oLK.LKMaritalStatusPK)
                    orderby oLK.DescriptionText ascending

                    select oLK;
            
            List<GenericLKTableResults> oList = new List<GenericLKTableResults>();
            foreach(LKMaritalStatus oLK in x)
            {
                GenericLKTableResults oGLK = new GenericLKTableResults();
                oGLK.Pk = oLK.LKMaritalStatusPK;
                oGLK.Description = oLK.DescriptionText;
                oList.Add(oGLK);
            }
            return oList;

        }

        public string GetDescriptionByPK(int pk)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x =
                        from oLK in oDB.LKMaritalStatus
                        where oLK.LKMaritalStatusPK == pk
                        select oLK.DescriptionText;
                List<string> oList = x.ToList();
                return (oList.Count == 0) ? "" : oList[0];
            }
        }

        public int InsertRecord(string description)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                LKMaritalStatus oLK = new LKMaritalStatus();
                oLK.ActiveInd = true;
                oLK.DescriptionText = description;
                oLK.LastUpdUser = UserInformation.UserName;
                oDB.LKMaritalStatus.InsertOnSubmit(oLK);
                oDB.SubmitChanges();
                return oLK.LKMaritalStatusPK;
            }

            
        }

        public bool ModifyRecord(int pk, string description)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x = (from oLK in oDB.LKMaritalStatus
                         where oLK.LKMaritalStatusPK == pk
                         select oLK).Single();
                x.DescriptionText = description;
                x.LastUpdUser = UserInformation.UserName;
                oDB.SubmitChanges();
            }

            return true;
        }

        public bool SetToInactive(int pk)
        {
            using (ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString()))
            {
                var x = (from oLK in oDB.LKMaritalStatus
                         where oLK.LKMaritalStatusPK == pk
                         select oLK).Single();
                x.ActiveInd = false;
                x.LastUpdUser = UserInformation.UserName;
                oDB.SubmitChanges();
            }

            return true;
        }
        #endregion
    }
}
